#include "stdafx.h"
#include "FieldServerSession.h"
#include "PacketHandler.h"
#include <PacketStruct.h>
#include <PublicMath.h>
#include "ServerSessionManager.h"
#include "AgentServer.h"
#include "Village.h"
#include "ZoneManager.h"
#include <MapInfoParser.h>

FieldServerSession::FieldServerSession()
{
}

FieldServerSession::~FieldServerSession()
{
}

VOID FieldServerSession::init()
{
	ServerSession::init();

	g_AgentServer.StartClientSideListen();

	CreateVillage();
}

VOID FieldServerSession::CreateVillage()
{
	MAPINFO_HASH & rHash = MapInfoParser::Instance()->GetMapInfoHash();
	for( MAPINFO_HASH_ITR it = rHash.begin() ; it != rHash.end() ; ++it )
	{
		sMAPINFO * pInfo = (*it);
		if( pInfo->byMKind == eZONETYPE_VILLAGE )
		{
			ZoneInterface * pZone = g_ZoneManager.FindVillage( pInfo->MapCode );
			if( pZone )
			{
				pZone->Create();	
			}
			else
			{
				RC::eROOM_RESULT rt = g_ZoneManager.CreateVillage( pInfo->MapCode, NULL );
				ASSERT( rt == RC::RC_ROOM_SUCCESS );
			}
		}		
	}
}

VOID FieldServerSession::OnRecv( BYTE *pMsg, WORD wSize )
{
	g_PacketHander.ParsePacket_AG( this, (MSG_BASE *)pMsg, wSize );
}

VOID FieldServerSession::OnDisconnect()
{
	ServerSession::removeRelatedUsers();

	ServerSession::OnDisconnect();
}

VOID FieldServerSession::OnLogString( char *pszLog )
{
	LogToFile( "NetworkLog_AgentServer.txt", "[FieldSession] %s", pszLog );
}